package com.inossem.warehousestorage_inossem.activity.storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.storage.StorageManageMatrAdapter;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrActivity extends BaseActivity {

    private TextView title,confirmBtn;

    private PullToRefreshLayout refresh;

    private PullableListView list;

    private StorageManageMatrAdapter adapter;
    private List<StorageManageMatrBean.DataBean.MaterielListBean> materielList;

    private StorageManageMatrBean information;


    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        //清空sp的物料整理信息
        SPUtils.removeInformation(this,SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION);

        setContentView(R.layout.activity_storage_manage_matr);
        initView(this);
        initData(this);

    }
    private void initView(Activity activity) {

        ActivityUtils.initBackAndFunction(activity, R.string.activity_storage_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if(information!=null&&StorageManageUtils.getUpCount(information)>0){
                    alertClear();
                }else{
                    finish();
                }
            }
        });
        title = (TextView) activity.findViewById(R.id.headerTitle);
        list = (PullableListView) activity.findViewById(R.id.list);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.refresh);
        confirmBtn = (TextView) activity.findViewById(R.id.confirmBtn);

        list.setPullDownFlag(false);
        list.setPullUpFlag(false);


    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_storage_manage_matr_title);
        materielList = new ArrayList<StorageManageMatrBean.DataBean.MaterielListBean>();
        adapter = new StorageManageMatrAdapter(this, materielList);
        this.list.setAdapter(adapter);

        setmScanListener((String scanResult, int length) -> {
            LogUtils.i("scanResult:" + scanResult + "---length:" + length);
            String batchNumber = QRCodeUtils.DecryptQRCode(scanResult, QRCodeConstant.PARAMETER_BATCH_NUMBER);
            adapter.doSearch(batchNumber);
        },  QRCodeConstant.TYPE_BATCH);

        confirmBtn.setOnClickListener( new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {

                if(information==null||StorageManageUtils.getUpCount(information)<=0){
                    Utils.middleToast(activity, getResources().getString(R.string.activity_up_frame_overview_commit_prompt));
                }else{
                    Intent intent =new Intent(activity,StorageManageMatrCommitActivity.class);
                    activity.startActivity(intent);
                }

            }
        });



    }

    @Override
    protected void onResumeImpl() {
        getData(this);
    }

    private void getData(Activity activity) {
        information = SPUtils.getInformation(activity, StorageManageMatrBean.class, SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION);
        if(information!=null){
            adapter.setList(information.getData().getMaterielList());
            adapter.notifyDataSetChanged();
            //存在可提交的整理则显示提交按钮
            if(StorageManageUtils.getUpCount(information)>0){
                confirmBtn.setVisibility(View.VISIBLE);
            }

        }

    }

    @Override
    public void onBackPressed() {
        if(information!=null&&StorageManageUtils.getUpCount(information)>0){
            alertClear();
        }else{
            finish();
        }
    }

    private void alertClear() {
        DialogUtils.getConfirmDialog(this, this.getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                StorageManageMatrActivity.super.onBackPressed();
            }
        }).show();
    }
}
